from django.db import models
from Doctor_django.utils.models import BaseModel
from stdimage import StdImageField
from django.utils.safestring import mark_safe
# 不支持上传文件
from ckeditor.fields import RichTextField
# 支持上传文件
from ckeditor_uploader.fields import RichTextUploadingField


# 大众健康-健康分类模型类
class HealthCategoryModel(BaseModel):
    name = models.CharField(max_length=200, unique=True, verbose_name='健康分类')
    remark = models.TextField(default="", blank=True, null=True, verbose_name="分类描述信息")

    class Meta:
        db_table = "dx_health_category"
        verbose_name_plural = "大众健康-健康分类"


# 大众健康-疾病分类模型类
class HealthDiseaseModel(BaseModel):
    name = models.CharField(max_length=200, verbose_name='疾病分类名称')
    remark = models.TextField(default="", blank=True, null=True, verbose_name="疾病分类信息描述")
    healthCategory = models.ForeignKey(to=HealthCategoryModel, related_name="health_disease_list", null=True, blank=True, on_delete=models.DO_NOTHING, verbose_name='健康')

    class Meta:
        db_table = "dx_health_disease"
        verbose_name_plural = "大众健康-疾病分类"


# 生活手册-生活分类模型类
class LifeCategoryModel(BaseModel):
    name = models.CharField(max_length=200, unique=True, verbose_name='生活分类')
    remark = models.TextField(default="", blank=True, null=True, verbose_name="分类描述信息")

    class Meta:
        db_table = "dx_life_category"
        verbose_name_plural = "生活手册-生活分类"


# 生活手册-生活技巧模型类
class LifeDiseaseModel(BaseModel):
    name = models.CharField(max_length=200, verbose_name='生活技巧名称')
    remark = models.TextField(default="", blank=True, null=True, verbose_name="生活技巧信息描述")
    lifeCategory = models.ForeignKey(to=LifeCategoryModel, related_name="life_disease_list", null=True, blank=True, on_delete=models.DO_NOTHING, verbose_name='生活')

    class Meta:
        db_table = "dx_life_disease"
        verbose_name_plural = "大众健康-生活技巧"


# 健康信息模型类
class HealthInfoModel(models.Model):
    title = models.CharField(max_length=255, verbose_name='健康文章标题')
    content = RichTextUploadingField(verbose_name='健康文章内容')
    remark = models.TextField(default="", blank=True, null=True, verbose_name="疾病分类信息描述")
    author = models.CharField(max_length=100, verbose_name='文章作者')
    create_date = models.DateField(auto_now_add=True, verbose_name='添加时间')
    update_date = models.DateField(auto_now=True, verbose_name='修改时间')
    cover = StdImageField(upload_to='health/%Y/', blank=True, null=True, verbose_name='健康文章封面',
                                 variations={
                                       # 'large': (600, 400),
                                       'thumb_800x800': {'width': 800, 'height': 800},
                                       # 'medium': (300, 200),
                                       'thumb_400x400': {'width': 400, 'height': 400},
                                       # 'thumbnail': (100, 100, True),
                                       'thumb_50x50': {'width': 50, 'height': 50},
                                })
    is_pass = models.BooleanField(default=False, verbose_name='是否通过审核')
    health_disease = models.ForeignKey(to=HealthDiseaseModel, related_name='health_info_list', on_delete=models.CASCADE, null=True, blank=True, db_constraint=False, verbose_name='疾病分类')

    class Meta:
        db_table = 'health_info'
        verbose_name_plural = '健康分类详细信息表'

    def __str__(self):
        return self.title

    # 返回一个图片image标签， 给管理站点返回；站点无法加载图片，返回一个image给站点
    def health_cover_small(self):
        # 小图展示
        return mark_safe(f'<img alt="" src="{self.cover.thumb_50x50.url}"></img>')

    cover.short_description = '健康文章封面'
    # 图片是否显示
    cover.allow_tags = True
    # 设置排序顺序
    cover.admin_order_field = "health_cover"


# 生活信息模型类
class LifeInfoModel(models.Model):
    title = models.CharField(max_length=255, verbose_name='生活文章标题')
    content = RichTextUploadingField(verbose_name='生活文章内容')
    remark = models.TextField(default="", blank=True, null=True, verbose_name="生活技巧信息描述")
    author = models.CharField(max_length=100, verbose_name='文章作者')
    create_date = models.DateField(auto_now_add=True, verbose_name='添加时间')
    update_date = models.DateField(auto_now=True, verbose_name='修改时间')
    cover = StdImageField(upload_to='life/%Y/', blank=True, null=True, verbose_name='生活文章封面',
                                 variations={
                                       # 'large': (600, 400),
                                       'thumb_800x800': {'width': 800, 'height': 800},
                                       # 'medium': (300, 200),
                                       'thumb_400x400': {'width': 400, 'height': 400},
                                       # 'thumbnail': (100, 100, True),
                                       'thumb_50x50': {'width': 50, 'height': 50},
                                })
    is_pass = models.BooleanField(default=False, verbose_name='是否通过审核')
    life_disease = models.ForeignKey(to=LifeDiseaseModel, related_name='life_info_list', on_delete=models.CASCADE, null=True, blank=True, db_constraint=False, verbose_name='生活技巧')

    class Meta:
        db_table = 'life_info'
        verbose_name_plural = '生活技巧详细信息表'

    def __str__(self):
        return self.title

    # 返回一个图片image标签， 给管理站点返回；站点无法加载图片，返回一个image给站点
    def life_cover_small(self):
        # 小图展示
        return mark_safe(f'<img alt="" src="{self.cover.thumb_50x50.url}"></img>')

    cover.short_description = '生活文章封面'
    # 图片是否显示
    cover.allow_tags = True
    # 设置排序顺序
    cover.admin_order_field = "cover"
